import com.alibaba.fastjson.JSON;
import com.model.Student;
import com.model.study;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * @PackageName:PACKAGE_NAME
 * @ClassName:text
 * @Description:
 * @author:zhangzhanheng
 * @date: 2020/12/24 18:29
 */
@SpringBootTest(classes = text.class)
public class text {

    /**
     * @return void
     * @Author zhangzhanheng
     * @Description //这是序列化与反序列化demo
     * @Date 18:40 2020/12/24
     * @Param []
     **/
    @Test
    public void text() throws Exception {
//        List<study> list= new ArrayList<>(4);
//        list.add(new study("1",1,"1"));
//        list.add(new study("2",2,"2"));
//        list.add(new study("3",3,"3"));
//        list.add(new study("4",4,"4"));
//        list.add(new study("5",5,"5"));
//
//
//        FileOutputStream fs = new FileOutputStream("D:\\foo");
//        ObjectOutputStream os = new ObjectOutputStream(fs);
//        os.writeObject(list);
//        os.close();
//
//        FileInputStream fi = new FileInputStream("D:\\foo");
//        ObjectInputStream is = new ObjectInputStream(fi);
//        List<study> box = (List<study>)is.readObject();
//        is.close();

    }

    /**
     * @return void
     * @Author zhangzhanheng
     * @Description //这里是深拷贝与浅拷贝
     * @Date 18:41 2020/12/24
     * @Param []
     **/
    @Test
    public void cloneDemo() throws CloneNotSupportedException {
        Student studentOld = new Student("zzh", 12);
        study studyOld = new study();
        studyOld.setMath("11");
        studentOld.setStudy(studyOld);

        Student new1 = (Student) studentOld.clone();
        studentOld.getStudy().setMath("22");
        studentOld.setAgeStudents(20);

        System.out.println("old math string:" + studentOld.getStudy().getMath());
        System.out.println("new1 math string:" + new1.getStudy().getMath());
        System.out.println("old age int:" + studentOld.getAgeStudents());
        System.out.println("newl age int :" + new1.getAgeStudents());
        System.out.println("newl study :" + new1.getStudy());
        System.out.print("old study: " + studentOld.getStudy());
    }

    @Test
    public void file() throws ParseException {
//        System.out.println(((1 & -1) == 1));
        Thread t1 = new Thread("t1"){
            public void run() {
                System.out.println(Thread.currentThread().getName());

            }
        };
        t1.start();
        System.out.println("do some");
    }

    @Test
    public void forea() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            stringBuilder.append(i);
        }
        System.out.println(stringBuilder.toString());
        System.out.println(Objects.hashCode(stringBuilder.toString()));
    }

    public static int serach(int[] arr, int key) {
        int start = 0;
        int end = arr.length - 1;
        while (start <= end) {
            int middle = (start + end) / 2;
            if (key > arr[middle]) {
                start = middle + 1;
            } else if (key < arr[middle]) {
                end = middle - 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    @Test
    public void HashMapDemo() {
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "1");
        map.put("1", "1");
        map.put("1", "12");
        System.out.println(map);
    }

    @Test
    public void demo() {
        Long updateTimeEnd = LocalDate.now().atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        System.out.println(updateTimeEnd);
    }

    @Test
    public void isPalindrome(int x) {
//        Unsafe unsafe = UnsafeUtils.getUnsafe();
        Callable a = new Callable() {
            @Override
            public Object call() throws Exception {
                return null;
            }
        };
    }

    @Test
    public void P() {
        TreeSet<Integer> set = new TreeSet<>();
        TreeSet<Integer> subSet = new TreeSet<>();
        for (int i = 606; i < 613; i++) {
            if (i % 2 == 0) {
                set.add(i);
            }
        }
        subSet = (TreeSet) set.subSet(608, true, 611, true);
        set.add(629);
        System.out.println(set + "---" + subSet);
    }

    @Test
    public void one1() {

        List<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        int b = 0;
        for (Integer integer : a) {
            b = b + integer;
        }

    }

    private static final int year = LocalDateTime.now().getYear();
    private static final int month = LocalDateTime.now().getMonthValue();

    @Test
    public void two() {
        List<String> a = new ArrayList<>();
        a.add("xxx");
        a.add("wqs1");
        System.out.println(JSON.toJSONString(a));
    }

    @Test
    public void three() {
        String a = "abc";
        String b = "ab" + "c";
        String c = new String("abc");
        String d = new String("abce");
        String e = new String(a + b);
        String f = a + b;
        String h = "abcabc";
        System.out.println(a == a.intern());
        System.out.println(a == b);
        System.out.println(b == b.intern());
        System.out.println(b == c.intern());
        System.out.println(b == c);
        System.out.println(c == c.intern());
        System.out.println(d == d.intern());
        System.out.println(e == e.intern());
        System.out.println(e.intern() == f.intern());
        System.out.println(f == h);
        System.out.println(f.intern() == h);
    }

    @Test
    public void Excel() throws GeneralSecurityException, IOException {
        String FILE = "d:/1.xls";
        POIFSFileSystem fs = new POIFSFileSystem();
        EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);

        Encryptor enc = info.getEncryptor();
        enc.confirmPassword("pass");

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("sheet1");
        sheet.createRow(0).createCell(0).setCellValue("data");

        // write the workbook into the encrypted OutputStream
        OutputStream encos = enc.getDataStream(fs);
        workbook.write(encos);
        workbook.close();
        encos.close(); // this is necessary before writing out the FileSystem

        OutputStream os = new FileOutputStream(FILE);
        fs.writeFilesystem(os);
        os.close();
        fs.close();

    }

    private static int i = 0;
    private static int count = 10000;//设置100，200。。。1000，10000,100000 查看结果

    @Test
    public void forDemo() {
        //消费者线程
        Thread t1 = new Thread(() -> {
            ArrayList list = new ArrayList();
            for (int y = 1; y <= count; y++) {
                list.add(y);
            }
            while (true) {
                if (list.contains(i)) {
                    System.out.println("i=========" + i);
                    break;
                }
            }
        }, "consumer");
        t1.start();

        //生产者线程
        Thread t = new Thread(() -> {
            shortWait(200000000);
            //生产完毕，设置信号量，通知消费线程
            for (int x = 1; x <= count; x++) {
                i = x;
            }
        }, "producer");
        t.start();
    }

    public static void shortWait(long interval) {
        long start = System.nanoTime();
        long end;
        do {
            end = System.nanoTime();
        } while (start + interval >= end);
    }


}
